package cn.bincker.mybatis.plus.encrypt;

import cn.bincker.mybatis.encrypt.core.EncryptCache;
import cn.bincker.mybatis.encrypt.entity.EncryptProperty;
import cn.bincker.mybatis.plus.encrypt.entity.Demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.lang.NonNull;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@SpringBootApplication
public class MybatisPlusEncryptBootStarterApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusEncryptBootStarterApplication.class, args);
    }

    @Bean
    public EncryptCache<Demo> encryptCache(){
        return new EncryptCache<Demo>() {
            private final Map<Long, Long> cache = new ConcurrentHashMap<>();
            @Override
            public boolean support(EncryptProperty property) {
                return property.getType() == Demo.class && "longTypeField".equals(property.getName());
            }

            @Override
            public Object get(EncryptProperty encryptProperty, Demo target) {
                return cache.get(target.getId());
            }

            @Override
            public void put(EncryptProperty encryptProperty, Demo target, Object value) {
                cache.put(target.getId(), (Long) value);
            }
        };
    }

}
